package org.jeecg.modules.baseData.controller;

import java.util.Arrays;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.baseData.dto.BindCardDTO;
import org.jeecg.modules.baseData.dto.CancelBindCardDTO;
import org.jeecg.modules.baseData.entity.BeiDouZhongDuan;
import org.jeecg.modules.baseData.dto.BeiDouZhongDuanDTO;
import org.jeecg.modules.baseData.service.IBeiDouZhongDuanService;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 北斗终端
 * @Author: wangfupeng
 * @Date:   2021-02-02
 * @Version: V1.0
 */
@Api(tags="北斗终端")
@RestController
@RequestMapping("/beidouzhongduan/tbBeiDouZhongDuan")
@Slf4j
public class BeiDouZhongDuanController extends JeecgController<BeiDouZhongDuan, IBeiDouZhongDuanService> {
	@Autowired
	private IBeiDouZhongDuanService tbBeiDouZhongDuanService;
	
	/**
	 * 分页列表查询
	 *
	 * @param tbBeiDouZhongDuanDTO
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@AutoLog(value = "北斗终端-分页列表查询")
	@ApiOperation(value="北斗终端-分页列表查询", notes="北斗终端-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BeiDouZhongDuanDTO tbBeiDouZhongDuanDTO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
								   ) {
		Page<BeiDouZhongDuan> page = new Page<BeiDouZhongDuan>(pageNo, pageSize);
		IPage<HashMap<String, Object>> pageList = tbBeiDouZhongDuanService.getPageList(page, tbBeiDouZhongDuanDTO);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param beiDouZhongDuan
	 * @return
	 */
	@AutoLog(value = "北斗终端-添加")
	@ApiOperation(value="北斗终端-添加", notes="北斗终端-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BeiDouZhongDuan beiDouZhongDuan) {
		tbBeiDouZhongDuanService.save(beiDouZhongDuan);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param beiDouZhongDuan
	 * @return
	 */
	@AutoLog(value = "北斗终端-编辑")
	@ApiOperation(value="北斗终端-编辑", notes="北斗终端-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BeiDouZhongDuan beiDouZhongDuan) {
		tbBeiDouZhongDuanService.updateById(beiDouZhongDuan);
		return Result.OK("编辑成功!");
	}

	 /**
	  * 北斗终端-取消绑定物联网卡
	  *
	  * @param cancelBindCardDTO 北斗终端和物联网卡的关联id
	  */
	@AutoLog(value = "北斗终端-取消绑定物联网卡")
	@ApiOperation(value="北斗终端-取消绑定物联网卡", notes="北斗终端-取消绑定物联网卡")
	@PostMapping(value = "/cancelBindCard")
	public Result<?> cancelBindCard(@RequestBody CancelBindCardDTO cancelBindCardDTO) {
		tbBeiDouZhongDuanService.cancelBindCard(cancelBindCardDTO.getWuLianWangKaReBeiDouZhongDuanId());
		return Result.OK("取消绑定成功!");
	}
	/**
	 *  北斗终端-绑定物联网卡
	 *
	 * @param bindCardDTO
	 * @return
	 */
	@AutoLog(value = "北斗终端-绑定物联网卡")
	@ApiOperation(value="北斗终端-绑定物联网卡", notes="北斗终端-绑定物联网卡")
	@PostMapping(value = "/bindCard")
	public Result<?> bindCard(@RequestBody BindCardDTO bindCardDTO) {
		tbBeiDouZhongDuanService.bindCard(bindCardDTO.getTerminalId(),bindCardDTO.getCardId());
		return Result.OK("绑定成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "北斗终端-通过id删除")
	@ApiOperation(value="北斗终端-通过id删除", notes="北斗终端-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		tbBeiDouZhongDuanService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "北斗终端-批量删除")
	@ApiOperation(value="北斗终端-批量删除", notes="北斗终端-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tbBeiDouZhongDuanService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "北斗终端-通过id查询")
	@ApiOperation(value="北斗终端-通过id查询", notes="北斗终端-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BeiDouZhongDuan beiDouZhongDuan = tbBeiDouZhongDuanService.getById(id);
		if(beiDouZhongDuan ==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(beiDouZhongDuan);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param beiDouZhongDuan
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BeiDouZhongDuan beiDouZhongDuan) {
        return super.exportXls(request, beiDouZhongDuan, BeiDouZhongDuan.class, "北斗终端");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BeiDouZhongDuan.class);
    }

}
